﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Iesi.Collections;
using Blazing.BusinessObjects.Tagging;
using Blazing.BusinessObjects.Assets;
using Iesi.Collections.Generic;
using Blazing.WebFacade;
using System.Collections.Generic;
using System.Diagnostics;

namespace Blazing.UI.Cms.Admin.Usercontrols
{
    public partial class AssetGrid : System.Web.UI.UserControl
    {
        [Serializable]
        protected struct AssetTag
        {
            private int _id;
            public int TagID
            {
                get { return _id; }
                set { _id = value; }
            }

            private string _name;
            public string Name
            {
                get { return _name; }
                set { _name = value; }
            }

            private string _description;
            public string Description
            {
                get { return _description; }
                set { _description = value; }
            }

            private Asset _asset;
            public Asset Asset
            {
                get { return _asset; }
                set { _asset = value; }
            }

            public AssetTag(int tagID, string name, string description, Asset asset)
                : this()
            {
                TagID = tagID;
                Name = name;
                Description = description;
                Asset = asset;
            }
        }
        
        private ISet<TaxonomyTag> viewStateTags
        {
            get
            {
                if (ViewState["viewStateTags"] == null)
                    return new HashedSet<TaxonomyTag>();
                return ViewState["viewStateTags"] as ISet<TaxonomyTag>;
            }
            set
            {
                ViewState["viewStateTags"] = value;
            }
        }

        private List<AssetTag> viewStateAssetTags
        {
            get
            {
                if (ViewState["viewStateAssetTags"] == null)
                    return new List<AssetTag>();
                return ViewState["viewStateAssetTags"] as List<AssetTag>;
            }
            set
            {
                ViewState["viewStateAssetTags"] = value;
            }
        }

        public int ThumbWidth
        {
            get
            {
                if (ViewState["viewStateThumbWidth"] == null)
                    return 0;
                return (int)ViewState["viewStateThumbWidth"];
            }
            set
            {
                ViewState["viewStateThumbWidth"] = value;
            }
        }

        public int ThumbHeight
        {
            get
            {
                if (ViewState["viewStateThumbHeight"] == null)
                    return 0;
                return (int)ViewState["viewStateThumbHeight"];
            }
            set
            {
                ViewState["viewStateThumbHeight"] = value;
            }
        }

        public List<TaxonomyTag> TagsToFilterBy
        {
             get
            {
                if (ViewState["viewStateFilterTags"] == null)
                    return new List<TaxonomyTag>();
                return ViewState["viewStateFilterTags"] as List<TaxonomyTag>;
            }
            set
            {
                ViewState["viewStateFilterTags"] = value;
                BindTags();
            }
        }

        public string TaxonomyToFilterBy
        {
            get
            {
                if (ViewState["viewStateFilterTax"] == null)
                    return String.Empty;
                return ViewState["viewStateFilterTax"].ToString();
            }
            set
            {
                ViewState["viewStateFilterTax"] = value;
                BindTags();
            }
        }

        public ISet<TaxonomyTag> TaxonomyTags
        {
            get
            {
                //UpdateRemovedItems();
                return viewStateTags;
            }
            set
            {
                viewStateTags = value;
                BindTags();
            }
        } 

        protected void Page_Load(object sender, EventArgs e)
        {
            
        }

        private void BindTags()
        {
            ISet<TaxonomyTag> tagList = new HashedSet<TaxonomyTag>();
            AssetController aCon = new AssetController();
            List<AssetTag> assetTags = new List<AssetTag>();
            List<TaxonomyTag> filter = new List<TaxonomyTag>();

            if (viewStateTags.Count > 0)
            {
                foreach (TaxonomyTag tag in viewStateTags)
                {
                    filter.Clear();
                    filter.Add(tag);
                    filter.AddRange(TagsToFilterBy);
                    tagList.Add(TaxonomyTag.Load(tag.ID));
                    IList<Asset> tagAssets = aCon.GetAssetsByTags(filter);
                    foreach (Asset a in tagAssets)
                    {
                        if (TaxonomyToFilterBy != String.Empty && tag.Taxonomy.Name == TaxonomyToFilterBy)
                            assetTags.Add(new AssetTag(tag.ID, tag.Slug, tag.Description, a));
                    }
                }
            }

            viewStateAssetTags = assetTags;
            viewStateTags = tagList;
            repeaterTags.DataSource = viewStateAssetTags;
            repeaterTags.DataBind();
        }

        protected void repeaterTags_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            AssetTag aTag = (AssetTag)e.Item.DataItem;
            Image img = new Image();
            if (ThumbHeight > 0 && ThumbWidth > 0)
                img.ImageUrl = String.Format("/Thumbnail.ashx?File={0}&Width={1}&Height={2}",
                    aTag.Asset.FilePath + aTag.Asset.FileName, ThumbWidth, ThumbHeight);
            else
                img.ImageUrl = aTag.Asset.FilePath + aTag.Asset.FileName;
            img.AlternateText = aTag.Asset.Description;
            e.Item.Controls.Add(img);
        }
    }
}